package com.cooper.base.service;

import com.cooper.base.model.SysRole;
import com.cooper.base.model.SysUserRole;
import com.cooper.common.core.service.BaseService;
import com.cooper.common.exception.GeneralException;
import com.cooper.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户角色Service
 */
@Service("com.cooper.base.service.SysUserRoleService")
public class SysUserRoleService extends BaseService<SysUserRole> {

    @Autowired
    private SysRoleService sysRoleService;

    public List<SysRole> listByUser(String userId) {
        if (StringUtil.isEmptyString(userId)) {
            throw new GeneralException("用户Id为空");
        }
        Example userRoleExample = new Example(SysUserRole.class);
        userRoleExample.createCriteria().andEqualTo("userId", userId);

        return mapper.selectByExample(userRoleExample).stream().map(sysUserRole -> {
            return sysRoleService.selectByKey(sysUserRole.getRoleId());
        }).collect(Collectors.toList());
    }
}
